package com.yh.ldzl.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.web.page.TableSupport;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.api.BasicService;
import com.ruoyi.system.api.domain.BasicProduct;
import com.ruoyi.system.api.domain.BasicWlgl;
import com.yh.ldzl.domain.QcTemplateIndex;
import com.yh.ldzl.domain.QcTemplateProduct;
import com.yh.ldzl.service.QcTemplateIndexService;
import com.yh.ldzl.service.QcTemplateProductService;
import com.yh.ldzl.service.QcTemplateService;
import com.yh.ldzl.domain.QcTemplate;
import com.ruoyi.common.core.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.stream.XMLOutputFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检测方案模块Controller
 *
 * @author ruoyi
 * @date 2024-08-18
 */
@RequestMapping("/qc/qcTemplate")
@RestController
public class QcTemplateController extends BaseController {

    @Autowired
    private QcTemplateService qcTemplateService;

    @Autowired
    private QcTemplateIndexService qcTemplateIndexService;

    @Autowired
    private QcTemplateProductService qcTemplateProductService;

    @Resource
    private BasicService basicService;


    /**
     * 查询检测方案模块列表
     */
    @PostMapping("/selectQcTemplateList")
    @Log(title = "查询检测方案列表", businessType = BusinessType.INSERT)
    public TableDataInfo selectQcTemplateList(@RequestBody QcTemplate qcTemplate) {

        System.out.println("selectQcTemplateList" + qcTemplate);
        startPage();
        List<QcTemplate> list = qcTemplateService.selectQcTemplateList(qcTemplate);
        return getDataTable(list);

    }

    /**
     * 查询检测方案模块检测项关联列表
     */
    @PostMapping("/selectQcTemplateIndexList")
    @Log(title = "查询检测方案检测项关联列表", businessType = BusinessType.INSERT)
    public TableDataInfo selectQcTemplateIndexList(@RequestBody QcTemplateIndex qcTemplateIndex) {
        System.out.println("selectQcTemplateIndexList" + qcTemplateIndex);
        startPage();
        int customPageSize = 5;
        PageDomain pageDomain = TableSupport.buildPageRequest();
        pageDomain.setPageSize(customPageSize);
        List<QcTemplateIndex> list = qcTemplateIndexService.selectQcTemplateIndexList(qcTemplateIndex);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 查询检测方案模块产品关联列表
     */
    @PostMapping("/selectQcTemplateProductList")
    @Log(title = "查询检测方案产品关联列表", businessType = BusinessType.INSERT)
    public TableDataInfo selectQcTemplateProductList(@RequestParam("template_id") Long template_id) {
        startPage();
        List<QcTemplateProduct> list = qcTemplateProductService.selectQcTemplateProductList(template_id);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 验证方案名称是否唯一
     */
    @PostMapping("/selectQcTemplateNameUnique")
    public boolean selectQcTemplateNameUnique(@RequestParam("template_name") String template_name) {
        boolean result = qcTemplateService.selectQcTemplateNameUnique(template_name);
        System.out.println("result:" + result);
        return result;
    }

    /**
     * 新增检测方案模块
     */
    @PostMapping("/insertQcTemplate")
    @Log(title = "新增检测方案", businessType = BusinessType.INSERT)
    public boolean insertQcTemplate(@RequestBody QcTemplate qcTemplate) {
        System.out.println(qcTemplate);
        return qcTemplateService.insertQcTemplate(qcTemplate);
    }

    /**
     * 修改检测方案模块
     */
    @PostMapping("/updateQcTemplate")
    @Log(title = "修改检测方案", businessType = BusinessType.INSERT)
    public boolean updateQcTemplate(@RequestBody QcTemplate qcTemplate) {
        return qcTemplateService.updateQcTemplate(qcTemplate);
    }

    /**
     * 删除检测方案模块
     */
    @PostMapping("/deleteQcTemplate")
    @Log(title = "删除检测方案", businessType = BusinessType.INSERT)
    public boolean deleteQcTemplate(@RequestBody List<Long> ids) {
        return qcTemplateService.deleteQcTemplate(ids);
    }

    /**
     * 新增检测项关联
     */
    @PostMapping("/insertQcTemplateIndex")
    @Log(title = "新增检测项关联", businessType = BusinessType.INSERT)
    public boolean insertQcTemplateIndex(@RequestBody QcTemplateIndex qcTemplateIndex) {
        return qcTemplateIndexService.insertQcTemplateIndex(qcTemplateIndex);
    }

    /**
     * 修改检测项关联
     */
    @PostMapping("/updateQcTemplateIndex")
    @Log(title = "修改检测项关联", businessType = BusinessType.INSERT)
    public boolean updateQcTemplateIndex(@RequestBody QcTemplateIndex qcTemplateIndex) {
        return qcTemplateIndexService.updateQcTemplateIndex(qcTemplateIndex);
    }

    /**
     * 验证检测项关联名称是否唯一
     */
    @PostMapping("/selectQcTemplateIndexNameUnique")
    public boolean selectQcTemplateIndexNameUnique(@RequestParam("index_name") String index_name) {
        boolean result = qcTemplateIndexService.selectQcTemplateIndexNameUnique(index_name);
        return result;
    }

    /**
     * 删除检测项关联
     */
    @PostMapping("/deleteQcTemplateIndex")
    @Log(title = "删除检测项关联", businessType = BusinessType.INSERT)
    public boolean deleteQcTemplateIndex(@RequestBody List<Long> ids) {
        return qcTemplateIndexService.deleteQcTemplateIndex(ids);
    }

    /**
     * 验证物料产品名称是否唯一
     */
    @PostMapping("/selectQcTemplateProductNameUnique")
    @Log(title = "验证物料产品名称是否唯一", businessType = BusinessType.INSERT)
    public boolean selectQcTemplateProductNameUnique(@RequestBody Map<String, String> requestBody) {
        String materialName = requestBody.get("material_name");
        Long templateId = Long.parseLong(requestBody.get("template_id"));
        if (templateId == null) {
            throw new IllegalArgumentException("templateId is required");
        }
        if (materialName == null) {
            throw new IllegalArgumentException("material_name is required");
        }
        return qcTemplateProductService.selectQcTemplateProductNameUnique(materialName,templateId);
    }

    /**
     * 新增物料产品信息
     */
    @PostMapping("/insertQcTemplateProduct")
    @Log(title = "新增物料产品信息", businessType = BusinessType.INSERT)
    public boolean insertQcTemplate(@RequestBody QcTemplateProduct qcTemplateProduct) {
        return qcTemplateProductService.insertQcTemplateProduct(qcTemplateProduct);
    }

    /**
     * 修改物料产品信息
     */
    @PostMapping("/updateQcTemplateProduct")
    @Log(title = "修改物料产品信息", businessType = BusinessType.INSERT)
    public boolean updateQcTemplate(@RequestBody QcTemplateProduct qcTemplateProduct) {
        return qcTemplateProductService.updateQcTemplateProduct(qcTemplateProduct);
    }

    /**
     * 删除物料产品信息
     */
    @PostMapping("/deleteQcTemplateProduct")
    @Log(title = "删除物料产品信息", businessType = BusinessType.INSERT)
    public boolean deleteQcTemplateProduct(@RequestBody List<Long> ids) {
        return qcTemplateProductService.deleteQcTemplateProduct(ids);
    }

    //查询物料
    @PostMapping("/wlgl/list")
    public TableDataInfo list1(@RequestBody BasicWlgl basicWlgl) {

        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        // 将分页信息设置到查询条件对象中，准备随远程调用发送
        Map<String, Object> params = basicWlgl.getParams();
        if (params == null) {
            params = new HashMap<>();
        }
        System.out.println("pageNum:"+pageNum+",pageSize:"+pageSize);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        basicWlgl.setParams(params);
                
        return basicService.list(basicWlgl);


    }

    //查询产品
    @PostMapping("/pro/list")
    public TableDataInfo list(@RequestBody BasicProduct basicProduct) {
        
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        // 将分页信息设置到查询条件对象中，准备随远程调用发送
        Map<String, Object> params = basicProduct.getParams();
        if (params == null) {
            params = new HashMap<>();
        }
        System.out.println("pageNum:"+pageNum+",pageSize:"+pageSize);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        basicProduct.setParams(params);
                
        return basicService.list(basicProduct);
    }

    /**
     * 根据物料查询检测方案
     */
    @PostMapping("/selectQcTemplateProductMaterialCode")
    public QcTemplateProduct selectQcTemplateProductMaterialCode(@RequestParam("material_code") String material_code)
    {

            return qcTemplateProductService.selectQcTemplateProductMaterialCode(material_code);
    }

    /**
     * 根据产品查询检测方案
     */
    @PostMapping("/selectQcTemplateProductProductCode")
    public QcTemplateProduct selectQcTemplateProductProductCode(@RequestParam("product_id") Long product_id)
    {
        return qcTemplateProductService.selectQcTemplateProductProductCode(product_id);
    }

    @PostMapping("/selectQcTemplateProductCode")
    public QcTemplateProduct selectQcTemplateProductCode(String product_code) {
        return qcTemplateProductService.selectQcTemplateProductCode(product_code);
    }










}